﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HNQXYJ.Model;
using HNQXYJ.DAL;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.SessionState;
namespace HNQXYJ.BLL
{
    /// <summary>
    /// TerminalAsh 的摘要说明
    /// </summary>
    public class TerminalAsh : IHttpHandler, IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
 
            string action = context.Request["action"];
            string id = context.Request["id"];
            
            string sendaction=context.Request["sendaction"];
            //加载地图数据，给地图添加标注，在标注上显示的基本信息。
            if (action == "getDataForMap")
            {

                DataTable dt = SqlHelper.GetDataTable("TerminalGetDataForMap");
                List<Terminal> list = new List<Terminal>();
                string num =Convert.ToString( dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new Terminal()
                    {
                        Phone = Convert.ToString(dr["Phone"]),
                        TownName = Convert.ToString(dr["TownName"]),
                        Leader = Convert.ToString(dr["Leader"]),
                        LeaderPhone = Convert.ToString(dr["LeaderPhone"]),
                        Statuss = Convert.ToString(dr["Statuss"]),
                        Lng = Convert.ToDouble(dr["Lng"]),
                        Lat = Convert.ToDouble(dr["Lat"]),
                        AddDate = dr["AddDate"].ToString().Split(' ')[0]
                       
                       
                    });
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(list));
                 
            }
            //根据id删除信息
            if (id != "")
            {
                //context.Response.Write(id);
                int res = 0;
                int Id = Convert.ToInt32(id);
                string procName = "TerminalDeleteById";
                SqlParameter[] paras ={
                                          new SqlParameter("@Id",SqlDbType.Int)
                                     };
                paras[0].Value = Id;

                res = TerminalBLL.DeleteTerminalData(procName, paras);
                if (res > 0)
                {
                    context.Response.Redirect("../HN_Terminal/TerminalMgrRankLevel.aspx", true);

                }
                //else
                //{
                //    context.Response.Write("删除失败");
                //}
            }

            //向终端发送信息
            if (sendaction == "sendMsg")
            {
                string sessionName = context.Session["username"].ToString();
                string sessionRankLevel=context.Session["ranklevel"].ToString();
                string msg = context.Request["sendMsg"];
                string phone = context.Request["phone"];
                List<string> list = new List<string>();
                list.Add(phone);
                string sendTimes = context.Request["sendTimes"];
                SocketListen.BtnSendMsgToAll(list, sessionName, msg, sendTimes, sessionRankLevel);
                context.Response.Write("ok");
            }

            //群发信息
            if (action == "sendMsgGroup")
            {
                string sessionName = context.Session["username"].ToString();
                string sessionRankLevel = context.Session["ranklevel"].ToString();
                string msg = context.Request["sendMsg"];
                string sendTimes = context.Request["sendTimes"];
                string procName = "TerminalGetPhoneForSendMsg";
                DataTable dt = TerminalBLL.GetTerminalTable(procName);
                List<string> list = new List<string>();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        list.Add(dr["Phone"].ToString().Trim());
                    }
                    SocketListen.BtnSendMsgToAll(list, sessionName, msg, sendTimes, sessionRankLevel);
                    context.Response.Write("ok");
                }
               // context.Response.Write("ok");
            }

            //以组为单位发信息
            if (action == "sendMsgGroups")
            {
                string sessionName = context.Session["username"].ToString();
                string sessionRankLevel = context.Session["ranklevel"].ToString();
                string areaCode = context.Request["areaCode"];
                string msg = context.Request["sendMsg"];
                string sendTimes = context.Request["sendTimes"];
                string ranklevel = context.Session["ranklevel"].ToString();
                List<string> list = new List<string>();
                if (areaCode == "1")
                {
                    list = TerminalBLL.GetPhonesByRankLevel(ranklevel);
                }
                else
                {
                    list = TerminalBLL.GetPhonesByAreaCode(areaCode);
                }
                SocketListen.BtnSendMsgToAll(list, sessionName, msg, sendTimes, sessionRankLevel);
                context.Response.Write("ok");
            }

            //根据权限码提取终端列表
            if (action == "GetTerminalListByRankLevel")
            {
                string selfCode = context.Request["selfCode"];
                List<Terminal> list = new List<Terminal>();
                list = TerminalBLL.GetTerminlListByRankLevel(selfCode);
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(list));

            }

            //获取市、县的统计数据
            if (action == "GetTerminalTongJiData")
            {
                string selfCode=context.Request["selfCode"];//传入的县市的权限码
                selfCode = selfCode + "%";
               //声明一个数组，前三位存大喇叭的总数、在线数和离线数，后三位存LED大屏的总数、在线数据和总数
                int[] count = new int[6];
                count[0] = TerminalBLL.GetNumberALLByRankLevel(selfCode, "0");//大喇叭的总数
                count[1] = TerminalBLL.GetNumberByStatusAndCounty("在线",selfCode,"0");
                count[2] = TerminalBLL.GetNumberByStatusAndCounty("离线", selfCode, "0");
                count[3] = TerminalBLL.GetNumberALLByRankLevel(selfCode, "1");
                count[4] = TerminalBLL.GetNumberByStatusAndCounty("在线", selfCode, "1");
                count[5] = TerminalBLL.GetNumberByStatusAndCounty("离线", selfCode, "1");

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(count));

            }

        
           
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}